﻿using System;
using System.Collections.Generic;
using System.Threading;
using NLog;
using SellAvi.Model;
using SellAvi.Schemas;

namespace SellAvi.Classes
{
    class TaskActivator : AvitoTask
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private readonly AvitoSearchItem _avitoActivationItem;
        private readonly AvitoTaskStatus _avitoTaskStatus;
        private readonly ActivatorSchema _sps;


        public TaskActivator(AbstractModel am)
            : base(am)
        {
            _avitoActivationItem = am as AvitoSearchItem;
            _sps = new ActivatorSchema();
            _avitoTaskStatus = new AvitoTaskStatus();

        }


        public override string TaskName
        {
            get { return "активация"; }
        }

        public override string TaskDescription
        {
            get { return _avitoActivationItem.AdTitle; }
        }

        public override List<OperationDelegate> TaskExecutionPlan
        {
            get
            {
                return new List<OperationDelegate>()
                {
                    PublicationTimeout,
                    ActivateAd
                };
            }
        }

        public override string TaskCompletedUrl
        {
            get { return base.WebClient.ResponseUri.ToString(); }
        }

        private AvitoTaskStatus PublicationTimeout()
        {
            var commandName = String.Format("Отправка сообщения через {0} секунд ...", TaskDelay);
            Logger.Trace(commandName);
            _avitoTaskStatus.OperationStartTime = DateTime.Now;
            _avitoTaskStatus.OperationMessage = commandName;
            Thread.Sleep(1000);
            TaskDelay--;
            _avitoTaskStatus.OperationSucceeded = true;
            _avitoTaskStatus.OperationRepeatNeeded = (TaskDelay > 0) ? true : false;
            return _avitoTaskStatus;
        }

        private AvitoTaskStatus ActivateAd()
        {
            var commandName = String.Format("Отправка сообщения через {0} секунд ...", TaskDelay);
            Logger.Trace(commandName);
            _avitoTaskStatus.OperationStartTime = DateTime.Now;
            _avitoTaskStatus.OperationMessage = commandName;

            var activationUrl = String.Format(_sps.UrlActivationTemplate, _avitoActivationItem.AdId);
            WebClient.DownloadString(new Uri(activationUrl));
            if (!CheckIfPaymentNotNeeded())
            {
                _avitoTaskStatus.OperationSucceeded = false;
                _avitoTaskStatus.ErrorCode = 411;
            }
            else
            {
                _avitoTaskStatus.OperationSucceeded = true;
            }
            return _avitoTaskStatus;
        }

        private bool CheckIfPaymentNotNeeded()
        {
            if (WebClient.ResponseUri.GetLeftPart(UriPartial.Path) == "https://www.avito.ru/account/pay_fee")
            {
                return false;
            }
            return true;
        }
    }
}
